package com.framework.service.core.biz.impl;

import com.framework.common.Enums.CertTypeEnum;
import com.framework.service.core.dao.model.TouristOrgInfoSnap;
import com.framework.core.api.request.TouristOrgSnapReq;
import com.framework.service.core.biz.ITouristOrgBiz;
import com.framework.service.core.dao.mapper.CertInfoSnapMapper;
import com.framework.service.core.dao.mapper.TouristOrgInfoMapper;
import com.framework.service.core.dao.mapper.TouristOrgInfoSnapMapper;
import com.framework.service.core.dao.model.CertInfoSnap;
import com.framework.service.core.dao.model.TouristOrgInfoSnap;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author zhoubb
 * @version V1.0
 * @Date 2020/5/26 23:29
 * @since JDK 1.8
 */
@Component
public class TouristOrgBizImpl implements ITouristOrgBiz {

    @Autowired
    private TouristOrgInfoSnapMapper touristOrgInfoSnapMapper;

    @Autowired
    private TouristOrgInfoMapper touristOrgInfoMapper;

    @Autowired
    private CertInfoSnapMapper certInfoSnapMapper;

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    @Override
    public boolean createTouristOrgSnap(TouristOrgSnapReq touristOrgSnapReq) {

        // 新增机构基本信息
        TouristOrgInfoSnap touristOrgInfoSnap = new TouristOrgInfoSnap();
        BeanUtils.copyProperties(touristOrgSnapReq, touristOrgInfoSnap);
        touristOrgInfoSnap.setCreateTime(new Date());
        touristOrgInfoSnapMapper.insertSelective(touristOrgInfoSnap);
        // 新增证照信息
        List<CertInfoSnap> certInfoList = new ArrayList<>();
        CertInfoSnap certInfo = new CertInfoSnap();

        // 身份证正面照
        certInfo.setCertPath(touristOrgSnapReq.getIdCardFrontPath());
        certInfo.setCertType(CertTypeEnum.ID_CARD_FRONT.getCode());
        certInfo.setSourceAuditId(touristOrgInfoSnap.getOrgAuditId());
        certInfo.setCreateTime(new Date());
        certInfoList.add(certInfo);

        certInfo = new CertInfoSnap();
        // 身份证反面照
        certInfo.setCertPath(touristOrgSnapReq.getIdCardBackPath());
        certInfo.setCertType(CertTypeEnum.ID_CARD_BACK.getCode());
        certInfo.setSourceAuditId(touristOrgInfoSnap.getOrgAuditId());
        certInfo.setCreateTime(new Date());
        certInfoList.add(certInfo);

        certInfo = new CertInfoSnap();
        // 营业执照
        certInfo.setCertPath(touristOrgSnapReq.getBusinessLicensePath());
        certInfo.setCertType(CertTypeEnum.BUISNESS_LICENSE.getCode());
        certInfo.setSourceAuditId(touristOrgInfoSnap.getOrgAuditId());
        certInfo.setCreateTime(new Date());
        certInfoList.add(certInfo);

        certInfo = new CertInfoSnap();
        // 道路运输运营许可证
        certInfo.setCertPath(touristOrgSnapReq.getRoadTransLicensePath());
        certInfo.setCertType(CertTypeEnum.ROAD_TRANS_LICENSE.getCode());
        certInfo.setSourceAuditId(touristOrgInfoSnap.getOrgAuditId());
        certInfo.setCreateTime(new Date());
        certInfoList.add(certInfo);

        certInfoSnapMapper.insertList(certInfoList);

        return true;
    }
}
